<template>
  <div class="page-header-index-wide">
    <a-card :loading="loading" :bordered="false" :body-style="{padding: '0'}">
      <div class="salesCard">
        <a-tabs  default-active-key="1" size="large" @change="callback" :tab-bar-style="{marginBottom: '24px', paddingLeft: '16px'}">
          <div class="extra-wrapper" slot="tabBarExtraContent">
            <div class="extra-item">
              <a @click="handleEdit()">今日</a>
              <a @click="handleEditWeek()">本周</a>
              <a @click="handleEditMonth()">本月</a>
              <a @click="handleEditYear()">本年</a>
            </div>
            <a-range-picker  :value="createValue" format="YYYY-MM-DD" @change="createChange" :style="{width: '256px'}" />
          </div>
          <a-tab-pane loading="true" tab="收益" key="1">
            <a-row>
              <a-col :xl="24" :lg="12" :md="12" :sm="24" :xs="24">
                <bar title="金额" yaxisText="金额(元)" :dataSource="barData"/>
              </a-col>
            </a-row>
          </a-tab-pane>
          <a-tab-pane tab="订单量" key="2">
            <a-row>
              <a-col :xl="24" :lg="12" :md="12" :sm="24" :xs="24">
                <bar title="单" yaxisText="单" :dataSource="barData"/>
              </a-col>
            </a-row>
          </a-tab-pane>
        </a-tabs>
      </div>
    </a-card>

    <a-row>
      <a-col :span="24">
        <a-card :loading="loading" :bordered="false" title="最近一周访问量统计" :style="{ marginTop: '24px' }">
          <a-row>
            <a-col :span="6">
              <head-info title="今日IP" :content="loginfo.todayIp"></head-info>
            </a-col>
            <a-col :span="2">
              <a-spin class='circle-cust'>
                <a-icon slot="indicator" type="environment" style="font-size: 24px"  />
              </a-spin>
            </a-col>
            <a-col :span="6">
              <head-info title="今日访问" :content="loginfo.todayVisitCount"></head-info>
            </a-col>
            <a-col :span="2">
              <a-spin class='circle-cust'>
                <a-icon slot="indicator" type="team" style="font-size: 24px"  />
              </a-spin>
            </a-col>
            <a-col :span="6">
              <head-info title="总访问量" :content="loginfo.totalVisitCount"></head-info>
            </a-col>
            <a-col :span="2">
              <a-spin class='circle-cust'>
                <a-icon slot="indicator" type="rise" style="font-size: 24px"  />
              </a-spin>
            </a-col>
          </a-row>
          <line-chart-multid :fields="visitFields" :dataSource="visitInfo"></line-chart-multid>
        </a-card>
      </a-col>
    </a-row>
  </div>
</template>

<script>
  import ChartCard from '@/components/ChartCard'
  import ACol from "ant-design-vue/es/grid/Col"
  import ATooltip from "ant-design-vue/es/tooltip/Tooltip"
  import MiniProgress from '@/components/chart/MiniProgress'
  import LineChartMultid from '@/components/chart/LineChartMultid'
  import HeadInfo from '@/components/tools/HeadInfo.vue'
  import Trend from '@/components/Trend'
  import { getLoginfo,getVisitInfo } from '@/api/api'
  import MiniBar from '@/components/chart/MiniBar'
  import Bar from '@/components/chart/Bar'
  import { httpAction, getAction } from '@/api/manage'

  const barData = []

  export default {
    name: "IndexChart",
    components: {
      ATooltip,
      ACol,
      ChartCard,
      MiniProgress,
      Trend,
      LineChartMultid,
      HeadInfo,
      MiniBar,
      Bar
    },
    data() {
      return {
        params:{
          beginTime:'',
          endTime:'',
        },
        getData1:'/home/getUserhour',
        getDataWeek:'/home/getUserWeek',
        getDataMonth:'/home/getUserMonth',
        getDataYear:'/home/getUserYear',
        getdate:'/home/getdate',
        getTotal:'home/getTotal',
        key:'1',
        loading: true,
        center: null,
        barData,
        methods:'handleEditWeek',
        loginfo:{},
        createValue: [],
        visitFields:['ip','visit'],
        visitInfo:[],
        indicator: '<a-icon type="loading" style="font-size: 24px" spin />'
      }
    },
    created() {
      setTimeout(() => {
        this.loading = !this.loading
        this.handleEditWeek();
      }, 1000)
      this.initLogInfo();
    },
    methods: {
      callback (key) {
        this.key = key;
        console.log(this.methods)
        if(this.methods==='handleEditWeek'){
          this.handleEditWeek();
        }
        if(this.methods==='handleEdit'){
          this.handleEdit();
        }
        if(this.methods==='handleEditMonth'){
          this.handleEditMonth();
        }
        if(this.methods==='handleEditYear'){
          this.handleEditYear();
        }
        if(this.methods==='getdate'){
          this.handleEditDate(this.params.beginTime,this.params.endTime);
        }
      },


      createChange(dates, dateStrings) {
        this.createValue = dates;
        this.params.beginTime = dateStrings[0]
        this.params.endTime = dateStrings[1]
        if(this.params.beginTime===''&&this.params.endTime===''){
          this.handleEditWeek();
        }else{
          this.handleEditDate(this.params.beginTime,this.params.endTime);
        }
      },
      handleEditDate(beginTime,endTime){
        this.methods = "getdate"
        getAction(this.getdate,{key:this.key,beginTime:beginTime,endTime:endTime}).then((res)=>{
          if(res.success){
            if(res.result.length>0){
              this.barData = res.result;
            }else{
              this.barData = this.dataSource
            }
          }else{
            this.barData = this.dataSource
          }
        }).finally(() => {
          // this.confirmLoading = false;
        })
      },

      //今日
      handleEdit(){
        this.methods='handleEdit';
        this.createValue=[];
        //获取今日24小时数据
        getAction(this.getData1,{key:this.key}).then((res)=>{
          if(res.success){
            if(res.result.length>0){
              this.barData = res.result;
            }else{
              this.barData = this.dataSource
            }
          }else{
            this.barData = this.dataSource
          }
        }).finally(() => {
          // this.confirmLoading = false;
        })
      },
      //本周
      handleEditWeek(){
        this.methods='handleEditWeek';
        this.createValue=[];
        getAction(this.getDataWeek,{key:this.key}).then((res)=>{
          if(res.success){
            if(res.result.length>0){
              this.barData = res.result;
            }else{
              this.barData = this.dataSource
            }
          }else{
            this.barData = this.dataSource
          }
        }).finally(() => {

        })
      },
      //本月
      handleEditMonth(){
        this.methods='handleEditMonth';
        this.createValue=[];
        getAction(this.getDataMonth,{key:this.key}).then((res)=>{
          if(res.success){
            if(res.result.length>0){
              this.barData = res.result;
            }else{
              this.barData = this.dataSource
            }
          }else{
            this.barData = this.dataSource
          }
        }).finally(() => {

        })
      },
      //本年
      handleEditYear(){
        this.methods='handleEditYear';
        this.createValue=[];
        getAction(this.getDataYear,{key:this.key}).then((res)=>{
          if(res.success){
            if(res.result.length>0){
              this.barData = res.result;
            }else{
              this.barData = this.dataSource
            }
          }else{
            this.barData = this.dataSource
          }
        }).finally(() => {

        })
      },


      initLogInfo () {
        getLoginfo(null).then((res)=>{
          if(res.success){
            Object.keys(res.result).forEach(key=>{
              res.result[key] =res.result[key]+""
            })
            this.loginfo = res.result;
          }
        })
        getVisitInfo().then(res=>{
          if(res.success){
             this.visitInfo = res.result;
           }
         })
      },
    }
  }
</script>

<style lang="less" scoped>
  .circle-cust{
    position: relative;
    top: 28px;
    left: -100%;
  }
  .extra-wrapper {
    line-height: 55px;
    padding-right: 24px;

    .extra-item {
      display: inline-block;
      margin-right: 24px;

      a {
        margin-left: 24px;
      }
    }
  }

  /* 首页访问量统计 */
  .head-info {
    position: relative;
    text-align: left;
    padding: 0 32px 0 0;
    min-width: 125px;

    &.center {
      text-align: center;
      padding: 0 32px;
    }

    span {
      color: rgba(0, 0, 0, .45);
      display: inline-block;
      font-size: .95rem;
      line-height: 42px;
      margin-bottom: 4px;
    }
    p {
      line-height: 42px;
      margin: 0;
      a {
        font-weight: 600;
        font-size: 1rem;
      }
    }
  }
</style>